//
//  JRSegmentViewController.m
//  JRSegmentControl
//
//  Created by 湛家荣 on 15/8/29.
//  Copyright (c) 2015年 湛家荣. All rights reserved.
//

#import "JRSegmentViewController.h"
#import "WuyefuwuViewController.h"


@interface JRSegmentViewController () <UIScrollViewDelegate, JRSegmentControlDelegate>
{
    CGFloat vcWidth;  // 每个子视图控制器的视图的宽
    CGFloat vcHeight; // 每个子视图控制器的视图的高
    
    JRSegmentControl *segment;
    
    BOOL _isDrag;
}

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation JRSegmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.view.backgroundColor = [UIColor whiteColor];
    
//    UIButton *button=[[UIButton alloc]init];
//    button.frame=CGRectMake(0, 0, 375, 64);
//    button.backgroundColor=[UIColor colorWithRed: 230/255.0 green:55/255.0 blue:55/255.0 alpha:1.0];
//    [self.view addSubview:button];
//    
//    
//    UILabel *labletext=[[UILabel alloc]init];
//    labletext.frame=CGRectMake(155, 34, 66, 14);
//    labletext.text=@"小区黄页";
//    labletext.adjustsFontSizeToFitWidth=YES;
//    labletext.textColor=[UIColor whiteColor] ;
//    [self.view addSubview:labletext];
//    
//    
//    
//    UIButton *button1=[[UIButton alloc]init ];//]WithFrame:CGRectMake(50, 50, 20, 20)];
//    button1.frame=CGRectMake(22, 35, 20, 20);
//    [button1 setBackgroundImage:[UIImage imageNamed:@"return@2x.png"] forState:UIControlStateNormal];
//    [button1 addTarget:self action:@selector(fanhui:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:button1];
//    
//    
//    

    
    self.navigationController.navigationBar.barTintColor = [[UIColor alloc] initWithRed:230/255.0 green:55/255.0 blue:55/255.0 alpha:1.0];
    
    
    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 20, 20);
    [button setBackgroundImage:[UIImage imageNamed:@"return@2x.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(fanhui) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = rightItem;
    
    
    
    
    
    [self setupScrollView];
    [self setupViewControllers];
    [self setupSegmentControl];
    
    
//            UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
//            button.frame = CGRectMake(0, 0, 20, 20);
//            [button setBackgroundImage:[UIImage imageNamed:@"return@2x.png"] forState:UIControlStateNormal];
//            [button addTarget:self action:@selector(fanhui) forControlEvents:UIControlEventTouchUpInside];
//            UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
//            self.navigationItem.leftBarButtonItem = rightItem;
//        
//    NSLog(@"显示了");
//    
//

    
    
}




- (IBAction)fanhui:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}




//- (IBAction)fanhui:(id)sender {
//
//    
//    [self dismissModalViewControllerAnimated:YES];
////    NSLog(@"fanhui");
////    WuyefuwuViewController *demoVC=[[WuyefuwuViewController alloc]init];
////    [self.navigationController pushViewController:demoVC animated:YES];
////    
////    
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (CGFloat)itemWidth
{
    if (_itemWidth == 0) {
        _itemWidth = 60.0f;
    }
    return _itemWidth;
}

- (CGFloat)itemHeight
{
    if (_itemHeight == 0) {
        _itemHeight = 30.0f;
    }
    return _itemHeight;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

/** 设置scrollView */
- (void)setupScrollView
{
    CGFloat Y = 0.0f;
    if (self.navigationController != nil && ![self.navigationController isNavigationBarHidden]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
        Y = 64.0f;
    }
    
    vcWidth = self.view.frame.size.width;
    vcHeight = self.view.frame.size.height - Y;
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, Y, vcWidth, vcHeight)];
    scrollView.contentSize = CGSizeMake(vcWidth * self.viewControllers.count, vcHeight);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator   = NO;
    scrollView.pagingEnabled = YES;
    scrollView.delegate      = self;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
}

/** 设置子视图控制器，这个方法必须在viewDidLoad方法里执行，否则子视图控制器各项属性为空 */
- (void)setupViewControllers
{
    int cnt = (int)self.viewControllers.count;
    for (int i = 0; i < cnt; i++) {
        UIViewController *vc = self.viewControllers[i];
        [self addChildViewController:vc];
        
        vc.view.frame = CGRectMake(vcWidth * i, 0, vcWidth, vcHeight);
        [self.scrollView addSubview:vc.view];
    }
}

/** 设置segment */
- (void)setupSegmentControl
{
    _itemWidth = 60.0f;
    // 设置titleView
    segment = [[JRSegmentControl alloc] initWithFrame:CGRectMake(0, 0, _itemWidth * 3, 30.0f)];
    segment.titles = self.titles;
    segment.cornerRadius = 5.0f;
    segment.titleColor = self.titleColor;
    segment.indicatorViewColor = self.indicatorViewColor;
    segment.backgroundColor = self.segmentBgColor;
    
    segment.delegate = self;
    self.navigationItem.titleView = segment;
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [segment selectedBegan];
    _isDrag = YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (_isDrag) {
        CGFloat percent = scrollView.contentOffset.x / scrollView.contentSize.width;
        
        [segment setIndicatorViewPercent:percent];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (decelerate == NO) {
        [segment selectedEnd];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    [segment setSelectedIndex:index];
    _isDrag = NO;
}

#pragma mark - JRSegmentControlDelegate

- (void)segmentControl:(JRSegmentControl *)segment didSelectedIndex:(NSInteger)index {
    CGFloat X = index * self.view.frame.size.width;
    [self.scrollView setContentOffset:CGPointMake(X, 0) animated:YES];
}

- (void)fanhui {
    
    [self.navigationController popViewControllerAnimated:YES];
    //[self dismissModalViewControllerAnimated:YES];
}




@end
